package com.example.ymm.modules1.service.impl.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.my.user.DbChatGroup;
import com.example.ymm.modules1.entity.my.user.DbChatGroupUser;
import com.example.ymm.modules1.entity.my.user.DbUser;
import com.example.ymm.modules1.mapper.my.user.DbChatGroupUserMapper;
import com.example.ymm.modules1.service.dd.RobotGroupMessagesService;
import com.example.ymm.modules1.service.dto.DbChatGroupDto;
import com.example.ymm.modules1.service.dto.DbChatGroupUserQueryCriteria;
import com.example.ymm.modules1.service.user.DbChatGroupService;
import com.example.ymm.modules1.service.user.DbChatGroupUserService;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
@Slf4j
public class DbChatGroupUserServiceImpl extends BaseServiceImpl<DbChatGroupUserMapper, DbChatGroupUser> implements DbChatGroupUserService {

    @Autowired
    DbChatGroupService dbChatGroupService;

    @Autowired
    RobotGroupMessagesService robotGroupMessagesService;

    @Override
    public TableList<DbChatGroupUser> list(DbChatGroupUserQueryCriteria criteria, YmPageDto pageDto) {
        IPage<DbChatGroupUser> page = new Page<DbChatGroupUser>(pageDto.getPage(),pageDto.getPageSize());
        baseMapper.selectPage(page, QueryHelpPlus.getPredicate(DbChatGroupUser.class, criteria));
        TableList<DbChatGroupUser> tableList=new TableList(page);
        return tableList;
    }

    public List<DbChatGroupUser> queryAll(DbChatGroupUserQueryCriteria criteria) {
        List<DbChatGroupUser> result =baseMapper.selectList(QueryHelpPlus.getPredicate(DbChatGroupUser.class, criteria));
        return result;
    }


    @Override
    public void syncGroupUser(DbChatGroup group, List<DbUser> dbUsers) {
        LambdaQueryWrapper<DbChatGroupUser> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(DbChatGroupUser::getOpenConversationId,group.getOpenConversationId());
        remove(queryWrapper);

        List<DbChatGroupUser> users=new ArrayList<>();
        dbUsers.forEach(e->{
            DbChatGroupUser groupUser=new DbChatGroupUser();
            groupUser.setUserId(e.getUserId());
            groupUser.setOpenConversationId(group.getOpenConversationId());
            users.add(groupUser);
        });
        saveOrUpdateBatch(users);
    }

    @Override
    public void addGroupUser(String openConversationId, String userIds) {
        String[] split = userIds.split(",");
        for(String userId: split){
            DbChatGroupUser chatGroupUser=new DbChatGroupUser();
            chatGroupUser.setOpenConversationId(openConversationId);
            chatGroupUser.setUserId(userId);
            saveOrUpdate(chatGroupUser);
        }

        DbChatGroup chatGroup = dbChatGroupService.getById(openConversationId);
        if(UtilValidate.isNotEmpty(chatGroup)){
            DbChatGroupDto group=new DbChatGroupDto();
            group.setAddUserIds(userIds);
            group.setOpenConversationId(openConversationId);
            group.setChatid(chatGroup.getChatid());
            robotGroupMessagesService.editGroupChat(group);
        }
    }
}
